﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ToastManager : Singleton<ToastManager>
{
	public GameObject toastCamera;
	bool isShowing = false;

    void Awake()
    {
        transform.position = new Vector3(3000f, 1500f, 0f);
		toastCamera = Instantiate(ResourceLoader.Load("Prefab/Common/Toast Camera")) as GameObject;
		toastCamera.transform.parent = transform;
        toastCamera.transform.localPosition = Vector3.zero;
        toastCamera.name = "ToastCamera";
        SceneController.SetAllChildsLayer(transform, LayerMask.NameToLayer("Toast"));
		gameObject.SetActive(false);
	}

	public void Show(GameObject toastObject, float duration)
	{
		if (isShowing) return;

		toastCamera.camera.targetTexture = null;

		isShowing = true;
		gameObject.SetActive(true);
		StartCoroutine(ShowCoroutine(toastObject, duration));
	}

	IEnumerator ShowCoroutine(GameObject toastObject, float duration)
	{
		GameObject g = Instantiate(toastObject) as GameObject;

		g.transform.parent = toastCamera.transform;
		g.transform.localPosition = Vector3.zero;
		g.transform.localRotation = Quaternion.identity;
		g.transform.localScale = Vector3.one;

		yield return new WaitForSeconds(duration);
		GameObject.DestroyImmediate(g);
		gameObject.SetActive(false);
		isShowing = false;
		yield return null;
	}

    #region 퀘스트, 업적 완료 토스트 처리
    Queue<GameObject> toastObjectList = new Queue<GameObject>();
    public void ShowQuestCompleteToast(bool isHold)
    {
        gameObject.SetActive(true);
        GameObject obj = Resources.Load("Prefab/Toast/Toast_Quest_notice") as GameObject;
        GameObject questCompleteToast = Instantiate(obj) as GameObject;
        //questCompleteToast.GetComponent<UIEventTrigger>().onRelease.Add(new EventDelegate(this, "GotoQuestMainScene"));
        GameObject obj2 = new GameObject();
        obj2.name = "parent";
        obj2.transform.parent = ToastManager.Instance.toastCamera.transform;
        obj2.transform.localPosition = new Vector3(0f, 0f, 200f);
        obj2.transform.localScale = Vector3.one;
        questCompleteToast.transform.parent = obj2.transform;
        questCompleteToast.transform.localScale = Vector3.one;
        if (questCompleteToast.transform.FindChild("Body") != null)
        {
            UILabel body = questCompleteToast.transform.FindChild("Body").GetComponent<UILabel>();
            if (body != null)
            {
                body.text = string.Format("[1aef9d]{0}[-] {1}", TextController.GetStringWithId(string.Format("questTitle{0}", UserData.Instance.userModel.questInfo.questId)), TextController.GetStringWithId("toastQuestChallengeclear"));
            }
        }

        if (!isHold)
        {
            if (currentQAToast != null)
                toastObjectList.Enqueue(questCompleteToast);
            else
            {
                currentQAToast = questCompleteToast;
                currentQAToast.SetActive(true);
                currentQAToast.GetComponent<Animation>().Play();
                SoundController.PlaySound("UI_Quest_notice");
                StartCoroutine("Co_ShowCompleteToast");
            }
        }
        else
            toastObjectList.Enqueue(questCompleteToast);
    }

    public void ShowAchievementCompleteToast(int achievementId, bool isHold)
    {
        gameObject.SetActive(true);
        GameObject obj = Resources.Load("Prefab/Toast/Toast_Achievement_notice") as GameObject;
        GameObject achievementCompleteToast = Instantiate(obj) as GameObject;
        //achievementCompleteToast.GetComponent<UIEventTrigger>().onRelease.Add(new EventDelegate(this, "GotoAchievementMainScene"));
        GameObject obj2 = new GameObject();
        obj2.name = "parent";
        obj2.transform.parent = ToastManager.Instance.toastCamera.transform;
        obj2.transform.localPosition = new Vector3(0f, 0f, 200f);
        obj2.transform.localScale = Vector3.one;
        achievementCompleteToast.transform.parent = obj2.transform;
        achievementCompleteToast.transform.localScale = Vector3.one;
        if (achievementCompleteToast.transform.FindChild("Body") != null)
        {
            UILabel body = achievementCompleteToast.transform.FindChild("Body").GetComponent<UILabel>();
            if (body != null)
            {
                body.text = string.Format("[1ab6ef]{0}[-] {1}", TextController.GetStringWithId(string.Format("achievetitle{0}", achievementId)), TextController.GetStringWithId("toastAchieveChallengeclear"));
            }
        }

        if (!isHold)
        {
            if (currentQAToast != null)
                toastObjectList.Enqueue(achievementCompleteToast);
            else
            {
                currentQAToast = achievementCompleteToast;
                currentQAToast.SetActive(true);
                currentQAToast.GetComponent<Animation>().Play();
                SoundController.PlaySound("UI_Achievement_notice");
                StartCoroutine("Co_ShowCompleteToast");
            }
        }
        else
            toastObjectList.Enqueue(achievementCompleteToast);
    }

    public void ShowQAToastInHoldList()
    {
        Debug.LogError("ShowQAToastInHoldList()");
        gameObject.SetActive(true);
        StartCoroutine("Co_ShowCompleteToast");
    }

    GameObject currentQAToast = null;
    IEnumerator Co_ShowCompleteToast()
    {
        while (currentQAToast != null && currentQAToast.GetComponent<Animation>().isPlaying)
            yield return null;
        if( currentQAToast != null )
            Destroy(currentQAToast);
        currentQAToast = null;

        Debug.LogError(toastObjectList.Count);
        while(toastObjectList.Count != 0)
        {
            if( !isOnGoingToQuestScene )
            {
                currentQAToast = toastObjectList.Dequeue();
                currentQAToast.SetActive(true);
                currentQAToast.GetComponent<Animation>().Play();
                if( currentQAToast.gameObject.name.Contains("Achievement") )
                    SoundController.PlaySound("UI_Achievement_notice");
                else if (currentQAToast.gameObject.name.Contains("Quest"))
                    SoundController.PlaySound("UI_Quest_notice");

                while(currentQAToast.GetComponent<Animation>().isPlaying)
                yield return null;

                Destroy(currentQAToast);
                currentQAToast = null;
            }
        }

        currentQAToast = null;
    }

    public bool isOnGoingToQuestScene = false;
    public void GotoQuestMainScene()
    {
        if (RecieaveInputObject.IsAbleControl)
        {
            if (BattleManager.Instance != null)
                return;
            
            isOnGoingToQuestScene = true;
            currentQAToast.SetActive(false);
            currentQAToast.GetComponent<Animation>().Stop();
            SceneController.LoadLevel("Quest");
        }
    }

    public void GotoAchievementMainScene()
    {
        if (RecieaveInputObject.IsAbleControl)
        {
            if (BattleManager.Instance != null)
                return;

            currentQAToast.SetActive(false);
            currentQAToast.GetComponent<Animation>().Stop();
            SceneController.LoadLevel("Achievement");
        }
    }

    #endregion
}
